package com.gph.etrip.routehandler;

import java.util.ArrayList;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class KMLHandler2 extends DefaultHandler{
	Placemark2 vPlacemark;
	boolean isPlacemark;
	boolean isRoute;
	boolean isStyle;
	boolean isStyleMap;
	private Stack vCurrent;
	private String mString;
	private Style2 mStyle;
	private StyleMap2 mStyleMap;
	private ArrayList<Style2> arrStyle;
	private ArrayList<StyleMap2> arrStyleMap;
	
	public KMLHandler2(){
		vPlacemark = new Placemark2();
		vCurrent = new Stack();		
		arrStyleMap = new ArrayList<StyleMap2>();
		arrStyle = new ArrayList<Style2>();
	}
	
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		vCurrent.push(localName);
		if (localName.equalsIgnoreCase("StyleMap")) {
			isStyleMap = true;
			isStyle = false;
			arrStyleMap.add(new StyleMap2());
			arrStyleMap.get(arrStyleMap.size()-1).smID = attributes.getQName(0);//styleMap id
			
		} else if (localName.equalsIgnoreCase("Style")) {
			isStyleMap = false;
			isStyle = true;
			if(localName.equalsIgnoreCase("hotspot")){
				mStyle.hotspotX = Float.parseFloat(attributes.getQName(0));
				mStyle.hotspotY = Float.parseFloat(attributes.getQName(1));
				mStyle.xHotspot = (attributes.getQName(2).equalsIgnoreCase("fraction"))? true : false;
				mStyle.xHotspot = (attributes.getQName(3).equalsIgnoreCase("fraction"))? true : false;				
			}else if (localName.equalsIgnoreCase("iconstyle")){
				
			}
			
		} else if (localName.equalsIgnoreCase("Placemark")){
			isPlacemark = true;
			if(localName.equalsIgnoreCase("Point")){
				isRoute = false;
			}else if(localName.equalsIgnoreCase("Point")){
				isRoute = true;
			}
			
		} 		
		
		mString = new String();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		String chars = new String(ch, start, length).trim();
		mString = mString.concat(chars);
	}

	@Override
	public void endElement(String uri, String localName, String qName)throws SAXException {
		if (mString.length() > 0) {
			//is StyleMap
			if (isStyleMap && localName.equalsIgnoreCase("pair")) {
				if (localName.equalsIgnoreCase("key")) {
					if(mString.equalsIgnoreCase("normal")){
						if(mString.equalsIgnoreCase("styleurl"))
						arrStyleMap.get(arrStyleMap.size()-1).smNID=mString;//normal icon url
					}else{
						arrStyleMap.get(arrStyleMap.size()-1).smHID=mString;//highlight icon url
					}
				}
			}
			//is Style
			else if (isStyle && localName.equalsIgnoreCase("iconstyle")){
				if(localName.equalsIgnoreCase("icon")){
					if(localName.equalsIgnoreCase("href")){
						mStyle.iconURL = mString;
					}
				}
				
			}
			
		}
	}

	private Point2[] addPoints(Point2[] points){
		Point2[] result = new Point2[points.length + 1];
		for (int i = 0; i < points.length; i++)
			result[i] = points[i];
		result[points.length] = new Point2();
		return result;
	}
	
	private void parseStyle(){
		
	}
	
	private void parsePlacemark(){
		
	}

}
